home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-10-18 | 7.4 KB | 329 lines | [TEXT/MPS ] |
- /*------------------------------------------------------------------------------
- # MacTutorApp
- #
- # A rudimentary application skeleton
- # J. Langowski / MacTutor 1989
- #------------------------------------------------------------------------------*/
- #include <Types.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <OSEvents.h>
- #include <Controls.h>
- #include <Windows.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Desk.h>
- #include <Scrap.h>
- #include <ToolUtils.h>
- #include <Memory.h>
- #include <SegLoad.h>
- #include <Files.h>
- #include <OSUtils.h>
- #include <Traps.h>
- #include <StdLib.h>
-
- // Constants, resource definitions, etc.
-
- #define kMinSize 48 // min heap needed in K
-
- #define rMenuBar 128 /* application's menu bar */
- #define rAboutAlert 128 /* about alert */
- #define rDocWindow 128 /* application's window */
-
- #define mApple 128 /* Apple menu */
- #define iAbout 1
-
- #define mFile 129 /* File menu */
- #define iNew 1
- #define iClose 4
- #define iQuit 12
-
- #define mEdit 130 /* Edit menu */
- #define iUndo 1
- #define iCut 3
- #define iCopy 4
- #define iPaste 5
- #define iClear 6
-
- #define myMenu 131 /* Sample menu */
- #define item1 1
- #define item2 2
- #define item3 3
- #define item5 5
-
- #include "TDocument.h"
- #include "TApplication.h"
- #include "MacTutorApp.h"
-
- // create and delete document windows
-
- // we pass the resID parameter up to our base class,
- // which actually creates the window for us
- TMacTutorDocument::TMacTutorDocument(short resID, StringPtr s) : (resID)
- {
- SetDisplayString(s);
- ShowWindow(fDocWindow); // Make sure the window is visible
- }
-
- TMacTutorDocument::~TMacTutorDocument(void)
- {
- HideWindow(fDocWindow);
- }
-
- void TMacTutorDocument::DoUpdate(void)
- {
- BeginUpdate(fDocWindow); // this sets up the visRgn
- if ( ! EmptyRgn(fDocWindow->visRgn) ) // draw if updating needs to be done
- {
- DrawWindow();
- }
- EndUpdate(fDocWindow);
- }
-
- // Draw the contents of an application window.
-
- void TMacTutorDocument::DrawWindow(void)
- {
- SetPort(fDocWindow);
- EraseRect(&fDocWindow->portRect);
-
- MoveTo(100,100);
- TextSize(18); TextFont(monaco);
- DrawString(fDisplayString);
-
- } // DrawWindow
-
- // Methods for our application class
- TMacTutorApp::TMacTutorApp(void)
- {
- Handle menuBar;
-
- // read menus into menu bar
- menuBar = GetNewMBar(rMenuBar);
- // install menus
- SetMenuBar(menuBar);
- DisposHandle(menuBar);
- // add DA names to Apple menu
- AddResMenu(GetMHandle(mApple), 'DRVR');
- DrawMenuBar();
-
- // create empty mouse region
- fMouseRgn = NewRgn();
- // create a single empty document
- DoNew();
- }
-
- // Tell TApplication class how much heap we need
- long TMacTutorApp::HeapNeeded(void)
- {
- return (kMinSize * 1024);
- }
-
- // Calculate a sleep value for WaitNextEvent. This takes into account the things
- // that DoIdle does with idle time.
-
- unsigned long TMacTutorApp::SleepVal(void)
- {
- unsigned long sleep;
- const long kSleepTime = 0x7fffffff; // a very large positive number
-
- sleep = kSleepTime; // default value for sleep
- if ((!fInBackground))
- {
- sleep = GetCaretTime(); // A reasonable time interval for MenuClocks, etc.
- }
- return sleep;
- }
-
- void TMacTutorApp::AdjustMenus(void)
- {
- WindowPtr frontmost;
- MenuHandle menu;
- Boolean undo,cutCopyClear,paste;
-
- TMacTutorDocument* fMacTutorCurDoc = (TMacTutorDocument*) fCurDoc;
-
- frontmost = FrontWindow();
-
- menu = GetMHandle(mFile);
- if ( fDocList->NumDocs() < kMaxOpenDocuments )
- EnableItem(menu, iNew); // New is enabled when we can open more documents
- else DisableItem(menu, iNew);
- if ( frontmost != (WindowPtr) nil ) // Close is enabled when there is a window to close
- EnableItem(menu, iClose);
- else DisableItem(menu, iClose);
-
- undo = false;
- cutCopyClear = false;
- paste = false;
-
- if ( fMacTutorCurDoc == nil )
- {
- undo = true; // all editing is enabled for DA windows
- cutCopyClear = true;
- paste = true;
- }
-
- menu = GetMHandle(mEdit);
- if ( undo )
- EnableItem(menu, iUndo);
- else
- DisableItem(menu, iUndo);
-
- if ( cutCopyClear )
- {
- EnableItem(menu, iCut);
- EnableItem(menu, iCopy);
- EnableItem(menu, iClear);
- }
- else
- {
- DisableItem(menu, iCut);
- DisableItem(menu, iCopy);
- DisableItem(menu, iClear);
- }
-
- if ( paste )
- EnableItem(menu, iPaste);
- else
- DisableItem(menu, iPaste);
-
- menu = GetMHandle(myMenu);
- EnableItem(menu, item1);
- EnableItem(menu, item2);
- EnableItem(menu, item3);
- EnableItem(menu, item5);
-
- CheckItem(menu, item1, false);
- CheckItem(menu, item2, false);
- CheckItem(menu, item3, false);
- CheckItem(menu, item5, false);
- CheckItem(menu, fMacTutorCurDoc->GetItemSelected(), true);
- } // AdjustMenus
-
-
- void TMacTutorApp::DoMenuCommand(short menuID, short menuItem)
- {
- short itemHit;
- Str255 daName;
- short daRefNum;
- WindowPtr window;
- TMacTutorDocument* fMacTutorCurDoc = (TMacTutorDocument*) fCurDoc;
-
- window = FrontWindow();
- switch ( menuID )
- {
- case mApple:
- switch ( menuItem )
- {
- case iAbout: // About box
- itemHit = Alert(rAboutAlert, nil);
- break;
- default: // DAs etc.
- GetItem(GetMHandle(mApple), menuItem, daName);
- daRefNum = OpenDeskAcc(daName);
- break;
- }
- break;
- case mFile:
- switch ( menuItem )
- {
- case iNew:
- DoNew();
- break;
- case iClose:
- if (fMacTutorCurDoc != nil)
- {
- fDocList->RemoveDoc(fMacTutorCurDoc);
- delete fMacTutorCurDoc;
- }
- else CloseDeskAcc(((WindowPeek) fWhichWindow)->windowKind);
- break;
- case iQuit:
- Terminate();
- break;
- }
- break;
- case mEdit: // call SystemEdit for DA editing & MultiFinder
- if ( !SystemEdit(menuItem-1) )
- {
- switch ( menuItem )
- {
- case iCut:
- break;
- case iCopy:
- break;
- case iPaste:
- break;
- case iClear:
- break;
- }
- }
- break;
- case myMenu:
- if (fMacTutorCurDoc != nil)
- {
- switch ( menuItem )
- {
- case item1:
- fMacTutorCurDoc->SetDisplayString("\pC++");
- break;
- case item2:
- fMacTutorCurDoc->SetDisplayString("\pSample");
- break;
- case item3:
- fMacTutorCurDoc->SetDisplayString("\pApplication");
- break;
- case item5:
- fMacTutorCurDoc->SetDisplayString("\pHave Fun");
- break;
- }
- fMacTutorCurDoc->SetItemSelected(menuItem);
- InvalRect(&(window->portRect));
- fMacTutorCurDoc->DoUpdate();
- }
- break;
- }
- HiliteMenu(0);
- } // DoMenuCommand
-
- // Create a new document and window.
- void TMacTutorApp::DoNew(void)
- {
- TMacTutorDocument* tMacTutorDoc;
-
- tMacTutorDoc = new TMacTutorDocument(rDocWindow,"\pNothing selected yet.");
- // if we didn't get an allocation error, add it to list
- if (tMacTutorDoc != nil)
- fDocList->AddDoc(tMacTutorDoc);
- } // DoNew
-
- void TMacTutorApp::Terminate(void)
- {
- ExitLoop();
- } // Terminate
-
- // Our application object, initialized in main(). We make it
- // global so our functions which don't belong to any class
- // can find the active document.
- TMacTutorApp *gTheApplication;
-
- // main is the entrypoint to the program
- int main(void)
- {
- // Create our application object. This MUST be the FIRST thing
- // done in main(), since it initializes the Toolbox for us.
- gTheApplication = new TMacTutorApp;
- if (gTheApplication == nil) // if we couldn't allocate object (impossible!?)
- return 0; // go back to Finder
-
- // Start our main event loop running. This won't return until user quits
- gTheApplication->EventLoop();
-
- // We always return a value, like good little ANSI worshippers
- return 0;
- }
-
-